package org.moore.controller;

import org.moore.model.Block;
import org.moore.model.Map;
import org.moore.model.User;
import org.moore.utils.Countdown;
import org.moore.utils.Data;
import org.moore.utils.SaveMapUtils;
import org.moore.view.SaveMapFrame;
import org.moore.view.SaveMapPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// 保存地图（存档）监听类
public class SaveMapListener implements ActionListener {
    private static SaveMapFrame saveMapFrame;
    // 定义按钮对应数字
    private static int num = 0;

    public SaveMapListener(SaveMapPanel saveMapPanel, SaveMapFrame saveMapFrame) {
        SaveMapListener.saveMapFrame = saveMapFrame;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 创建一个新的字体对象
        Font newFont = new Font("宋体", Font.PLAIN, 18);
        // 设置所有的OptionPane字体
        UIManager.put("OptionPane.messageFont", newFont);
        UIManager.put("OptionPane.buttonFont", newFont);
        // 获取动作命令
        String action = e.getActionCommand();
        // 展示等级模式
        String gameMode = "";
        if (Map.isMapType() == 1){
            gameMode = "步数";
        } else if (Map.isMapType() == 2){
            gameMode = "时间";
        }
        String gameLevel = "";
        if (Map.getLevel() == 1){
            gameLevel = "初级";
        } else if (Map.getLevel() == 2){
            gameLevel = "中级";
        } else if (Map.getLevel() == 3){
            gameLevel = "高级";
        }
        switch (action) {
            case "saveMap" :
                // 提示保存地图状态
                Object[] saveOptions = { "保存", "取消"};
                int saveInput = JOptionPane.showOptionDialog(null, "当前状态：\n游戏模式：" + gameMode + gameLevel +
                        "\n当前步数：" + Data.STEPS + "\n当前时间：" + Countdown.getSeconds() + "\n当前分数：" + Data.SCORE + "\n是否要保存当前状态？",
                        "保存存档", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, saveOptions, saveOptions[0]);
                if  (saveInput == 0) {
                    // 遍历获取当前地图消除物类型，将其存入新的数组
                    for (int i = 0; i < Map.getMap().length; i++) {
                        for (int j = 0; j < Map.getMap()[0].length; j++) {
                            Map.getSaveMap()[i][j] = Map.getMap()[i][j].getBlockType();
                        }
                    }
                    // 调用地图保存方法
                    SaveMapUtils.saveMap();
                }
                break;
            case "save1" :
                // 存档1
                num = 0;
                showMapData(num);
                break;
            case "save2" :
                // 存档2
                num = 1;
                showMapData(num);
                break;
            case "save3" :
                // 存档3
                num = 2;
                showMapData(num);
                break;
            case "save4" :
                // 存档4
                num = 3;
                showMapData(num);
                break;
            case "save5" :
                // 存档5
                num = 4;
                showMapData(num);
                break;
            case "save6" :
                // 存档6
                num = 5;
                showMapData(num);
                break;
        }
    }

    // 展示存档地图数据的方法
    public static void showMapData(int num){
        if(!SaveMapUtils.roadMapText(User.getId(),num).isEmpty()){
            // 定义数组
            int[][] array;
            // 取出每个消除物类型
            array  = new int[9][9];
            for (int i = 0; i < Map.getMap().length; i++) {
                for (int j = 0; j < Map.getMap()[0].length; j++) {
                    array[i][j] = SaveMapUtils.roadMap(User.getId(), num)[i][j];
                }
            }
            // 将取出的消除物赋值到地图中
            for (int i = 0; i < Map.getMap().length; i++) {
                for (int j = 0; j < Map.getMap()[0].length; j++) {
                    if (array[i][j] == 0) {
                        Map.getMap()[i][j] = new Block(0, i, j, array[i][j], new ImageIcon("Image/zhuankuai.jpgD").getImage());
                    } else {
                        Map.getMap()[i][j] = new Block(0, i, j, array[i][j], new ImageIcon("Image/IMG " + array[i][j] + ".png").getImage());
                    }
                    Map.getMap()[i][j].setY(Data.START_Y + i * Data.GRID);
                    Map.getMap()[i][j].setX(Data.START_X + j * Data.GRID);
                }
            }
            saveMapFrame.setVisible(false);
        } else {
            JOptionPane.showMessageDialog(null, "存档" + (num+1) + "没有存档！");
        }
    }
}
